package com.irdstudio.apicenter.console.dm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.stereotype.Component;

import com.irdstudio.apicenter.frame.core.jdbc.util.ConnectionManager;

@Component(value = "appDutyDao")
public class AppDutyDao {

	/**
	 * 根据主键查询单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public AppDuty queryWithKeys(String appId, String appDutyId)
			throws SQLException {
		AppDuty dc = null;
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM app_duty WHERE app_id=? and app_duty_id=?");
			ps.setString(1, appId);
			ps.setString(2, appDutyId);
			rs = ps.executeQuery();
			if (rs.next()) {
				dc = new AppDuty();
				dc.setAppId(rs.getString("app_id"));
				dc.setAppDutyId(rs.getString("app_duty_id"));
				dc.setAppDutyName(rs.getString("app_duty_name"));
				dc.setRemarks(rs.getString("remarks"));

			}
		} catch (SQLException e) {
			throw new SQLException("queryAppTradePrivateWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close2(rs, conn, ps);
		}
		return dc;
	}

	public int saveAppDuty(AppDuty appDuty) throws SQLException {

		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "insert into app_duty values(?,?,?,?);";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appDuty.getAppId());
			ps.setString(2, appDuty.getAppDutyId());
			ps.setString(3, appDuty.getAppDutyName());
			ps.setString(4, appDuty.getRemarks());
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("queryAppFrameStartWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;

	}

	/**
	 * 修改模块参数
	 * 
	 * @param appModuleParam
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int update(AppDuty appDuty) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "update  app_duty  set APP_DUTY_NAME=?,REMARKS=? where APP_ID=? and APP_DUTY_ID=? ";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appDuty.getAppDutyName());
			ps.setString(2, appDuty.getRemarks());
			ps.setString(3, appDuty.getAppId());
			ps.setString(4, appDuty.getAppDutyId());

			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("queryAppFrameStartWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;

	}

	public int delete(String appId, String appDutyId) throws SQLException {
		// TODO Auto-generated method stub
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "delete from  app_duty  where APP_ID=? and APP_DUTY_ID=? ";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appId);
			ps.setString(2, appDutyId);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("queryAppFrameStartWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close1(Connection conn, PreparedStatement thePs) {
		try {
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close2(ResultSet theRs, Connection conn,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
